package mahan.test.data.mongodb;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import mahan.data.model.Vendor;
import mahan.data.mongodb.MahanDB;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

public class VendorTestCase {
	private MahanDB mahanDb;

	@After
	public void clean() {
		mahanDb.get().dropDatabase();
	}

	@Before
	public void setup() {
		mahanDb = new MahanDB("localhost", 27017, "vendor");
		DB db = mahanDb.get();
		assertNotNull(db);
	}

	@SuppressWarnings("unchecked")
	@Test
	public void testVendor() throws Exception {
		clean();
		Vendor vendor = new Vendor(1111, "Ad");
		vendor.setAddress("123 c");
		vendor.setCity("c");
		vendor.setCountry("ZX");
		vendor.setEmailAddress("a@sss.com");
		vendor.setPhone("11111");
		vendor.setState("zx");
		vendor.setWebsite("a");
		vendor.setZip("234");

		// Store into DB
		BasicDBObject dbo = new BasicDBObject();
		dbo.putAll(vendor.get());
		DBCollection coll = mahanDb.createCollection("vendor");
		coll.save(dbo);

		// Retrieve from DB
		DBObject fromDB = coll.findOne();
		Vendor from = Vendor.instance();
		from.put(fromDB.toMap());

		assertEquals("Ad", from.getName());
		assertEquals(1111, from.getId());
		assertEquals("123 c", from.getAddress());
		assertEquals("c", from.getCity());
		assertEquals("ZX", from.getCountry());
		assertEquals("a@sss.com", from.getEmailAddress());
		assertEquals("11111", from.getPhone());
		assertEquals("zx", from.getState());
		assertEquals("a", from.getWebsite());
		assertEquals("234", from.getZip());
	}
}